package com.lichuang.dedao.curriculum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.dedao.curriculum.entity.CurriculumAudition;
import com.lichuang.dedao.curriculum.entity.User;
import com.lichuang.dedao.curriculum.fingnclient.UserFeignClient;
import com.lichuang.dedao.curriculum.mapper.CurriculumAuditionMapper;
import com.lichuang.dedao.curriculum.service.ICurriculumAuditionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 试听 服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2019-07-22
 */
@Service
public class CurriculumAuditionServiceImpl extends ServiceImpl<CurriculumAuditionMapper, CurriculumAudition> implements ICurriculumAuditionService {

    @Autowired
    private CurriculumAuditionMapper curriculumAuditionMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    QueryWrapper queryWrapper = new QueryWrapper();
    CurriculumAudition curriculumAudition = new CurriculumAudition();

    @Override
    public Integer insertCurriculumAudition(CurriculumAudition curriculumAudition,String loginToken) {
        User user = userFeignClient.getCurrentUser(loginToken);
        curriculumAudition.setCreateTime(LocalDateTime.now());
        curriculumAudition.setUserId(user.getUserId());
//        queryWrapper = new QueryWrapper();
//        curriculumAudition = new CurriculumAudition();
//        curriculumAudition.setUserId(user.getUserId());
//        curriculumAudition.setAuditionSectionId(curriculumAudition.getAuditionSectionId());
//        curriculumAudition.setCurriculumId(curriculumAudition.getCurriculumId());
//        queryWrapper.setEntity(curriculumAudition);
//        List<CurriculumAudition> curriculumAuditions = curriculumAuditionMapper.selectList(queryWrapper);
        if(getCountByUserIdAndCurriculum(loginToken,curriculumAudition.getCurriculumId(),
                curriculumAudition.getAuditionSectionId()) != 0){
            return 2;
        }
        return curriculumAuditionMapper.insertCurriculumAudition(curriculumAudition);
    }

    @Override
    public List<CurriculumAudition> getAllAuditionByUserId(String loginToken){
        User user = userFeignClient.getCurrentUser(loginToken);
        queryWrapper = new QueryWrapper();
        curriculumAudition = new CurriculumAudition();
        curriculumAudition.setUserId(user.getUserId());
        queryWrapper.setEntity(curriculumAudition);
        return curriculumAuditionMapper.selectList(queryWrapper);
    }

    @Override
    public Integer getCountByUserIdAndCurriculum(String loginToken,Integer curriculumId, Integer auditionSectionId) {
        User user = userFeignClient.getCurrentUser(loginToken);
        queryWrapper = new QueryWrapper();
        curriculumAudition = new CurriculumAudition();
        curriculumAudition.setUserId(user.getUserId());
        curriculumAudition.setAuditionSectionId(auditionSectionId);
        curriculumAudition.setCurriculumId(curriculumId);
        queryWrapper.setEntity(curriculumAudition);
        return curriculumAuditionMapper.selectCount(queryWrapper);
    }
}
